package com.sk.mylibrary;


import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import android.net.LocalServerSocket;
import android.net.LocalSocket;

import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;

import android.os.Looper;
import android.os.Process;
import android.os.SystemClock;
import android.util.Log;
import android.view.InputDevice;
import android.view.MotionEvent;

import java.io.PrintStream;

public class InjectServer {
    private int mInetSocketPort;
    static final int PRESS = 0;
    static final int SWIPE = 1;
    static final int KEYUP = 1;
    static final int KEYDOWN = 0;
    static int keyCount = 0;
    InputUtils mInputUtils = null;
    byte[] key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    byte[] key2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    //byte[][] key = new byte[2][2];

    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    int keyid = 0;
    ServerSocket mServerSocket = null;

    Socket receiver;
    InputStream ips;

    public InjectServer(int inetSocketPort) {
        mInetSocketPort = inetSocketPort;
    }

    public InjectServer() {
        mInetSocketPort = 10080;
        mInputUtils = new InputUtils();
    }

    public static void main(String[] args) {

        Process.setArgV0("com.example");
        //Log.d("InjectServer", "InjectServer: Starting ...");
        System.err.println("InjectServer: Starting ...");
        System.err.println("InjectService: start success!");
        InjectServer instance = null;
        try {
            instance = new InjectServer();
            instance.execute();
            // instance.onStartThread();
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

    }


    public void execute() {
        Log.d("InjectServer", "execute");
        ServerSocket mServerSocket = null;
        try {
            mServerSocket = new ServerSocket(mInetSocketPort);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (true) {
            try {
                // System.err.println("InjectServer x=="+ keygameButton.getX()+"y=="+keygameButton.getY());
                receiver = mServerSocket.accept();
                ips = receiver.getInputStream();

                //1.获取客户端发送的信息

                // ObjectInputStream ois = new ObjectInputStream(ips);



                while (true) {

                    int ret = ips.read(key);
                    // keygameButton  = (KeygameButton)ois.readObject();

                 /*
                    OutputStream ops = socket.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(ops);
                    dos.writeUTF(message);
                    dos.flush();*/

                    if(ret == -1)
                    {
                        ips.close();
                        receiver.close();
                        break;
                    }
                 /*   //if (faceButton == null) {
                        //4.释放资源
                        ois.close();
                        //dos.close();
                        mServerSocket.close();
                        receiver.close();
                   // }*/
                    // System.err.println("InjectServer x=="+ keygameButton.getX()+"y=="+keygameButton.getY());

                    keyid = key[0] & 0xFF;
                    x1 = key[2] & 0xFF | ((key[3] & 0xFF) << 8);
                    y1 = key[4] & 0xFF | ((key[5] & 0xFF) << 8);
                    x2 = key[6] & 0xFF | ((key[7] & 0xFF) << 8);
                    y2 = key[8] & 0xFF | ((key[9] & 0xFF) << 8);
                    // key[10] = 'B';
                    System.err.println("InjectServer localsocket position:" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + "command:" + key[0] + "," + "action" + key[1]);

                    if (x1 == 111) {
                        int array[] = new int[2];
                        array[2] = 99;
                    }

                 /*   if (KEYDOWN == key[1]) {
                        System.err.println("InjectServer down ");
                        if (x1 == 111) {
                            int array[] = new int[2];
                            array[2] = 99;
                        }
                        keyCount++;
                        mInputUtils.injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, keyid, MotionEvent.ACTION_DOWN, keyCount, x1, y1);

                        //mInputUtils.sendKeyDown(InputDevice.SOURCE_TOUCHSCREEN, x1+100, y1,1);
                    } else if (KEYUP == key[1]) {
                        System.err.println("InjectServer up");

                        mInputUtils.injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, keyid, MotionEvent.ACTION_UP, keyCount, x1, y1);
                        keyCount--;
                        //mInputUtils.sendKeyUp(InputDevice.SOURCE_TOUCHSCREEN, x1+100, y1,1);
                    }*/

                }


            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    }
}